// pages/service/order-list/index.js
var api = require("../../../../api.js"),
  QQMapWX = require("../../../../qqmap-wx.js"),
  app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentOrderType:1,
    currentItemId: "0",
    orderNumber: '',
    startTime: '',
    endTime: '',
    data: [],
    pageSize: 5,
    currentPageSize: 5
  },
  onLoad: function (options) {
    this.setData({
      currentItemId: options.currentItemId ? options.currentItemId:0
    });

    /**
     * 筛选传入参数
     * orderNumber 订单号
     * startTime: 开始时间
     * endTime: 结束时间
     */

    /**订单列表 */
    app.pageOnLoad(this);

    var that = this;
    that.setData({
      icons: wx.getStorageSync('site') + '/addons/sd_liferuning/tp/public/uploads/background',
    })
    // app.request({
    //   url: api.default.orderlist,
    //   data: {
    //     status: 0,
    //     uid: wx.getStorageSync('uid'),
    //     limit: that.data.pageSize
    //   },
    //   success: function (res) {
    //     var data = []
       
    //     that.setData({
    //       data: res.data,
         
    //     })
    //   }

    // })
    if (options.orderNumber) {
      that.setData({
        orderNumber: options.orderNumber
      });
    }
    if (options.startTime) {
      that.setData({
        startTime: options.startTime
      });
    }
    if (options.endTime) {
      that.setData({
        endTime: options.endTime
      });
    }
  },
  /**
  * 跳转功能页面
  */
  navToPage: function (e) {
    // console.log(e)
    //return false;
    var that = this;
    var _url = e.currentTarget.dataset.url + "?id=" + e.currentTarget.dataset.id + '&cid=' + e.currentTarget.id;
    if (!that.isLogin()) {
      that.gotoLogin();
    } else {
      wx.navigateTo({
        url: _url,
      })
    }
  },
  /**
   * 跳转用户页面
   */
  navToUserPage: function (e) {
    var that = this;
    if (!that.isLogin()) {
      that.gotoLogin();
    } else {
      wx.navigateTo({
        url: "/sd_liferuning/pages/constmer/user/index",
      })
    }
  },

  //自定义底部跳转
  naveClick: function (t) {
    app.navigatorClick(t, this);
  },
 onShow:function(){
   var that = this;

   that.orderlist()
   
 },
 orderlist:function(){
    var that=this;
    app.request({
      url: api.default.orderlists,
      data: {
        type: that.data.currentOrderType,
        status: that.data.currentItemId,
        uid: wx.getStorageSync('uid'),
        limit: that.data.currentPageSize
      },
      success: function (res) {
        var data = []
        //console.log('订单列表', res.data);
        that.setData({
          data: res.data,
        })
      }

    })
 },
  /**
    * 切换订单请求类型
    */
  selectCurrentOrderType: function (e) {
    var that = this;
    let _type = e.currentTarget.dataset.type;
    this.setData({
      currentOrderType: _type
    });
    that.orderlist()
  },


  changeSidebar: function () {
    var that = this;
    that.openSidebar();
  },
  selectSwiper: function (e) {
    var that = this;
    var val = e.currentTarget.dataset.listid;
    that.initCurrentPageSize();
    that.setData({
      currentItemId: val
    });
    var that = this;
    that.orderlist()
  },
  changeSwiper: function (e) {
    var that = this;
    var val = e.detail.current;
    that.initCurrentPageSize();
    that.setData({
      currentItemId: val
    });
    that.orderlist()
  },
  /**
   * 数据刷新
   */
  refreshData: function () {
    let _this = this;
    let _currentItemId = _this.data.currentItemId;  // 当前选中的ID
    let _pageSize = _this.data.pageSize;  // 每次加载多少条
    let _currentPageSize = _this.data.currentPageSize;  // 当前数据
    _currentPageSize+=_pageSize;
    _this.setData({
      currentPageSize: _currentPageSize
    });

    _this.sendRequestByCurrentPageSize({ currentItemId: _currentItemId, currentPageSize: _currentPageSize});
  },
  /**
   * 执行查询
   * @param currentItemId 当前查询的ID
   * @param currentPageSize 当前页面需要查询的总数
   */
  sendRequestByCurrentPageSize({currentItemId,currentPageSize}){
    var that=this;
    let _currentItemId = currentItemId;
    let _currentPageSize = currentPageSize;

    
    app.request({
      url: api.default.orderlist,
      data: {
        status: _currentItemId,
        uid: wx.getStorageSync('uid'),
        limit: _currentPageSize
      },
      success: function (res) {
        var data = []
        // console.log('订单列表', res.data);
        
        that.setData({
          data: res.data
        })
        wx.hideLoading()
      
        
     
      },
      fail:function(){
        wx.hideLoading()
      }
      
    })
  
   
  },
  /**
   * 初始化当页数据
   */
  initCurrentPageSize: function(){
    let _this = this;
    let _pageSize = _this.data.pageSize;  // 每次加载多少条
    _this.setData({
      currentPageSize: _pageSize
    });
  },
})